<template>
	<view style="padding: 20rpx;box-sizing: border-box;">
		<u-toast ref="uToast"></u-toast>
		<u--form labelPosition="left" labelWidth="80" labelAlign="center" :model="userInfo" :rules="rules"
			ref="userInfoForm">
			<u-form-item label="头像">
				<u-upload :fileList="fileList" @afterRead="afterRead" @delete="deletePic" name="uploadAvatar" :maxCount="1"></u-upload>
			</u-form-item>
			<u-form-item label="昵称" prop="nickName" borderBottom>
				<u--input v-model="userInfo.nickName" border="none"></u--input>
			</u-form-item>
			<u-form-item label="性别" borderBottom>
				<u-radio-group v-model="userInfo.gender">
					<u-radio label="男" :name="0" style="margin-right: 15px;"></u-radio>
					<u-radio label="女" :name="1"></u-radio>
				</u-radio-group>
			</u-form-item>
			<u-form-item label="手机号" prop="phoneNumber" borderBottom>
				<u--input v-model="userInfo.phoneNumber" border="none"></u--input>
			</u-form-item>
		</u--form>
		<u-button type="primary" text="提交" customStyle="margin-top: 20px" @click="submit"></u-button>
	</view>
</template>

<script>
	import {
		ptUrl
	} from '@/utils/env.js'
	export default {
		data() {
			return {
				ptUrl: ptUrl,
				userInfo: {},
				rules: {
					'nickName': [{
						type: 'string',
						required: true,
						message: '请填写姓名',
						trigger: ['blur', 'change']
					},{
						type: 'string',
						min: 2,
						required: true,
						message: '不低于2个字',
						trigger: ['change']
					},{
						type: 'string',
						max: 10,
						required: true,
						message: '不多于10个字',
						trigger: ['change']
					}],
					'phoneNumber': [{
						required: true,
						message: '请填写您的手机号码',
						trigger: ['blur', 'change']
					}, {
						pattern: /^1[3-9]\d{9}$/,
						message: '手机号码格式不正确',
						trigger: ['blur', 'change']
					}],
				},
				fileList: [],
				toast:{
					type: 'success',
					title: '修改成功',
					message: "修改成功",
					iconUrl: '@/static/success.png',
					url: '/pages/user/user',
				},
			}
		},
		onLoad() {
			this.getUserinfo(false)
		},
		onReady() {
			// 如果需要兼容微信小程序，并且校验规则中含有方法等，只能通过setRules方法设置规则
			this.$refs.userInfoForm.setRules(this.rules)
		},
		methods: {
			getUserinfo(refresh) {
				this.$http("/wx-user/userinfo", "GET", {
					refresh: refresh
				}).then(res => {
					this.userInfo = res.data.data
					this.fileList.push({
						status: 'success',
						message: '',
						url: ptUrl+this.userInfo.avatarUrl,
						thumb: ptUrl+this.userInfo.avatarUrl,
						type: 'image',
					})	
				})
			},
			submit() {
				let data = JSON.parse(JSON.stringify(this.userInfo))
				if(this.fileList.length && this.fileList[0].uploadUrl){
					data.avatarUrl = this.fileList[0].uploadUrl
				}
				this.$http("/wx-user/updateWxUser", "POST", data).then(res => {
					// console.log(res)
					this.fileList = []
					this.getUserinfo(true);
					this.showToast()
				})
			},
			showToast() {
				let params = this.toast
				this.$refs.uToast.show({
					...params,
					complete() {
						params.url && uni.switchTab({
							url: params.url
						})
					}
				})
			},
			async afterRead(event) {
				let file = event.file
				this.fileList.push({
					...file, 
					status: 'uploading',
					message: '上传中',
				})
				const url = await this.uploadFilePromise(file.url)
				this.fileList.splice(0,1,{
					...this.fileList[0],
					status: 'success',
					message: '',
					thumb: ptUrl+url,
					url: ptUrl+url,
					uploadUrl: url,
				})
				// console.log(this.fileList)
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					uni.uploadFile({
						url: ptUrl + '/wx-user/uploadAvatar',
						filePath: url,
						name: "file",
						header: {
							'Authorization': uni.getStorageSync('token')
						},
						success: (res) => {
							let data = JSON.parse(res.data)
							// console.log(res)
							if (data.code == 0) {
								resolve(data.data)
							} else {
								reject(data)
							}
						}
					})
				})
			},
			deletePic() {
				this.fileList.pop()
			},
		}
	}
</script>

<style>
</style>
